home *** CD-ROM | disk | FTP | other *** search
- Path: newsfeeds.ans.net!interaccess!usenet
- From: brianmcg@interaccess.com (Brian V. McGroarty)
- Newsgroups: comp.lang.c
- Subject: Re: array of struct
- Date: 20 Mar 1996 14:18:44 GMT
- Organization: Internet Squire
- Message-ID: <4ip444$g6h@nntp.interaccess.com>
- References: <Pine.LNX.3.91.960319173746.16221A-100000@larry.inf.net>
- Reply-To: brianmcg@interaccess.com
- NNTP-Posting-Host: d41-isdn.nhe.interaccess.com
- X-Newsreader: Internet Squire 1.20
-
- I'll assume that pointers to the strings are okay, i.e. you don't need them
- to be an actual part of the structure.
-
- I would allocate a pool of RAM which is one character larger than the file.
- By running through this range of memory and replacing all line ends with
- character 0 you create a table of individual strings. During the run
- through the file where you were replacing line ends you could either count
- the number of strings to determine how many structures you will need to
- allocate in an array, or you could count as well as place pointers to the
- beginning of each string in a temporary array of pointers which is
- guaranteed to be large enough to hold the entire set of pointers. If you
- have simply counted then you must run through the file again to fill the
- member of each structure which should contain a pointer to a string. If
- you have created the temporary array of pointers then you would copy these
- pointers from the array instead.
-
- Something else which I see people do from time to time is to allocate an
- array of items which is guaranteed to be large enough to hold a full set
- and resize with realloc() or to create a linked list, resulting in a new
- allocation for every single record. I don't like the realloc() method
- because it temporarily bloats your program's memory requirements and, thus,
- causes some virtual memory systems to thrash. A linked list is a good
- answer if you will be dynamically adding or removing data items, but it
- reduces the speed of random access to your data and adds extra
- inefficiencies due to the system's internal structuring of your memory list
- and on systems which allocate objects of a multiple of a certain size.
-
-
- Lawrence O'Leary wrote:
-
- >I have a question... though it is petty it bothers me greatly. :(
-
- >How would I create a runtime defined array of struct.
-
- >What I want to do is create each record in the array by reading a line
- >from a file and storing each field of the line into the record. Do this
- >for every line in the file and then where done. What I need to do is
- >create the array of the struct for the exact number of lines that are in
- >the file. This way I won't be wasting memory and I won't have to worry
- >about not having enough records to store the lines into.
-
- ---
- Brian Valters McGroarty -- brianmcg@bix.com
- phone/fax (847) 439-7714
-